package com.atguigu.srb.core.service.impl;

import com.atguigu.srb.core.enums.BorrowerStatusEnum;
import com.atguigu.srb.core.enums.IntegralEnum;
import com.atguigu.srb.core.mapper.*;
import com.atguigu.srb.core.pojo.entity.*;
import com.atguigu.srb.core.pojo.vo.BorrowerApprovalVO;
import com.atguigu.srb.core.pojo.vo.BorrowerAttachVO;
import com.atguigu.srb.core.pojo.vo.BorrowerDetailVO;
import com.atguigu.srb.core.pojo.vo.BorrowerVO;
import com.atguigu.srb.core.service.BorrowerService;
import com.atguigu.srb.core.service.DictService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 借款人 服务实现类
 * </p>
 *
 * @author Helen
 * @since 2021-09-22
 */
@Service
public class BorrowerServiceImpl extends ServiceImpl<BorrowerMapper, Borrower> implements BorrowerService {

    @Autowired
    UserInfoMapper userInfoMapper;

    @Autowired
    BorrowerAttachMapper borrowerAttachMapper;

    @Autowired
    DictMapper dictMapper;

    @Autowired
    DictService dictService;

    @Resource
    private UserIntegralMapper userIntegralMapper;


    @Override
    public void saveBorrowerVOByUserId(BorrowerVO borrowerVO, Long userId) {

        UserInfo userInfo = userInfoMapper.selectById(userId);

        // 保存额度申请信息
        Borrower borrower = new Borrower();
        BeanUtils.copyProperties(borrowerVO,borrower);
        borrower.setUserId(userId);
        borrower.setName(userInfo.getName());
        borrower.setIdCard(userInfo.getIdCard());
        borrower.setMobile(userInfo.getMobile());
        borrower.setStatus(BorrowerStatusEnum.AUTH_RUN.getStatus());
        borrower.setIsMarry(borrowerVO.getMarry()?1:0);
        baseMapper.insert(borrower);

        Long borrowerId = borrower.getId();

        // 保存额度申请附件信息
        List<BorrowerAttach> borrowerAttachList = borrowerVO.getBorrowerAttachList();
        for (BorrowerAttach borrowerAttach : borrowerAttachList) {
            borrowerAttach.setBorrowerId(borrowerId);
            borrowerAttachMapper.insert(borrowerAttach);
        }

        // 更新userInfo中的借款人认证状态borrow_auth_status
        userInfo.setBorrowAuthStatus(BorrowerStatusEnum.AUTH_RUN.getStatus());
        userInfoMapper.updateById(userInfo);

    }

    @Override
    public Page<Borrower> listPage(Page<Borrower> pageParam, String keyword) {

        QueryWrapper<Borrower> borrowerQueryWrapper = new QueryWrapper<>();
        // keyword模糊查询，可以是手机号，姓名，身份证
        borrowerQueryWrapper
                .like(!StringUtils.isEmpty(keyword),"mobile",keyword)
                .or().like(!StringUtils.isEmpty(keyword),"name",keyword)
                .or().like(!StringUtils.isEmpty(keyword),"id_card",keyword)
                .orderByDesc("id");
        Page<Borrower> borrowerPage = baseMapper.selectPage(pageParam, borrowerQueryWrapper);

        return borrowerPage;
    }

    @Override
    public BorrowerDetailVO show(Long id) {

        Borrower borrower = baseMapper.selectById(id);
        BorrowerDetailVO borrowerDetailVO = new BorrowerDetailVO();
        BeanUtils.copyProperties(borrower,borrowerDetailVO);
        borrowerDetailVO.setSex(borrower.getSex()==1?"男":"女");
        borrowerDetailVO.setMarry(borrower.getIsMarry()==1?"已婚":"未婚");
        borrowerDetailVO.setStatus(BorrowerStatusEnum.getMsgByStatus(borrower.getStatus()));
        borrowerDetailVO.setIdCard(borrower.getIdCard());
        borrowerDetailVO.setEducation(dictService.getNameByParentDictCodeAndValue("education",borrower.getEducation()));
        borrowerDetailVO.setIndustry(dictService.getNameByParentDictCodeAndValue("industry",borrower.getIndustry()));
        borrowerDetailVO.setIncome(dictService.getNameByParentDictCodeAndValue("income",borrower.getIncome()));
        borrowerDetailVO.setReturnSource(dictService.getNameByParentDictCodeAndValue("returnSource",borrower.getReturnSource()));
        borrowerDetailVO.setContactsRelation(dictService.getNameByParentDictCodeAndValue("relation",borrower.getContactsRelation()));
        QueryWrapper<BorrowerAttach> borrowerAttachQueryWrapper = new QueryWrapper<>();
        borrowerAttachQueryWrapper.eq("borrower_id",id);
        List<BorrowerAttach> borrowerAttaches = borrowerAttachMapper.selectList(borrowerAttachQueryWrapper);

        List<BorrowerAttachVO> borrowerAttachVOS = borrowerAttaches.stream().map(borrowerAttache->{
            BorrowerAttachVO borrowerAttachVO = new BorrowerAttachVO();
            BeanUtils.copyProperties(borrowerAttache,borrowerAttachVO);
            return borrowerAttachVO;
        }).collect(Collectors.toList());

        borrowerDetailVO.setBorrowerAttachVOList(borrowerAttachVOS);
        return borrowerDetailVO;
    }

    @Override
    public void approval(BorrowerApprovalVO borrowerApprovalVO) {

            //借款人认证状态
            Long borrowerId = borrowerApprovalVO.getBorrowerId();
            Borrower borrower = baseMapper.selectById(borrowerId);
            borrower.setStatus(borrowerApprovalVO.getStatus());
            baseMapper.updateById(borrower);

            Long userId = borrower.getUserId();
            UserInfo userInfo = userInfoMapper.selectById(userId);

            //添加积分
            UserIntegral userIntegral = new UserIntegral();
            userIntegral.setUserId(userId);
            userIntegral.setIntegral(borrowerApprovalVO.getInfoIntegral());
            userIntegral.setContent("借款人基本信息");
            userIntegralMapper.insert(userIntegral);

            int curIntegral = userInfo.getIntegral() + borrowerApprovalVO.getInfoIntegral();
            if(borrowerApprovalVO.getIsIdCardOk()) {
                curIntegral += IntegralEnum.BORROWER_IDCARD.getIntegral();
                userIntegral = new UserIntegral();
                userIntegral.setUserId(userId);
                userIntegral.setIntegral(IntegralEnum.BORROWER_IDCARD.getIntegral());
                userIntegral.setContent(IntegralEnum.BORROWER_IDCARD.getMsg());
                userIntegralMapper.insert(userIntegral);
            }

            if(borrowerApprovalVO.getIsHouseOk()) {
                curIntegral += IntegralEnum.BORROWER_HOUSE.getIntegral();
                userIntegral = new UserIntegral();
                userIntegral.setUserId(userId);
                userIntegral.setIntegral(IntegralEnum.BORROWER_HOUSE.getIntegral());
                userIntegral.setContent(IntegralEnum.BORROWER_HOUSE.getMsg());
                userIntegralMapper.insert(userIntegral);
            }

            if(borrowerApprovalVO.getIsCarOk()) {
                curIntegral += IntegralEnum.BORROWER_CAR.getIntegral();
                userIntegral = new UserIntegral();
                userIntegral.setUserId(userId);
                userIntegral.setIntegral(IntegralEnum.BORROWER_CAR.getIntegral());
                userIntegral.setContent(IntegralEnum.BORROWER_CAR.getMsg());
                userIntegralMapper.insert(userIntegral);
            }

            userInfo.setIntegral(curIntegral);
            //修改审核状态
            userInfo.setBorrowAuthStatus(borrowerApprovalVO.getStatus());
            userInfoMapper.updateById(userInfo);
    }

}
